package handler

import (
	"net/http"
	"strings"
  f "EventsApp/pkg/handler/helperFunctions"

	"github.com/gin-gonic/gin"
)

const (
  authorizationHeader = "Authorization"
  userCtx = "userId"
)

func userIdentity(c *gin.Context) {
  header := c.GetHeader(authorizationHeader)

  if header == "" {
    c.String(http.StatusUnauthorized, "empty auth header")
  }

  headerParts := strings.Split(header, " ")
  
  if len(headerParts) != 2 {
    c.String(http.StatusUnauthorized, "invalid auth header")
  }

  userId, err := f.ParseToken(headerParts[1])

  if err != nil {
    c.String(http.StatusUnauthorized, err.Error())
  }

  c.Set(userCtx, userId)
}
